//
//  ServerMainController.m
//  jboss-admin
//
//  Created by Christos Vasilakis on 26/06/2010.
//  Copyright 2010 All rights reserved.
//

#import "FetchJSONOperation.h"
#import "JBossServer.h"

#import "ServerMainController.h"
#import "DeploymentsViewController.h"
#import "ProgressViewController.h"

@implementation ServerMainController

@synthesize options;
@synthesize workQueue;
@synthesize server;
@synthesize spinner;

- (void)viewDidLoad {
	[super viewDidLoad];
	
	NSArray *array = [[NSArray alloc] initWithObjects: @"System Information",
					  @"Server Logs", @"Web Applications", @"Enterprise Applications",
					  @"Resource Adapters", @"Connection Factories", @"Data Sources",
					  @"JMS Destinations", @"Scripts", nil];
	
	self.options = array;
	
	[array release];
	
	ProgressViewController *theSpinner = [[ProgressViewController alloc] initWithNibName:@"ProgressView" bundle:nil];
	self.spinner = theSpinner;
	
	[theSpinner release];
			
	NSOperationQueue *theWorkQueue = [[NSOperationQueue alloc] init];
	self.workQueue = theWorkQueue;
	
	[theWorkQueue release];
		
	[self.workQueue setMaxConcurrentOperationCount:1];
	
	self.title = server.name;
	
	NSLog(@"ServerMainController viewDidLoad");
}

- (void)viewDidUnload {
	[super viewDidUnload];
	
	self.options = nil;
	self.workQueue = nil;
	self.server = nil;
	self.spinner = nil;

	NSLog(@"ServerMainController viewDidUnload");
}

- (void)dealloc {
	[options release];
	[workQueue release];
	[server release];
	//[spinner release];
		
	NSLog(@"ServerMainController deAlloc");
	
	[super dealloc];
}

#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.options count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"CellIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
	}
	
	NSUInteger row = [indexPath row];
	
	NSString *option = [self.options objectAtIndex:row];
	
	cell.textLabel.text = option;
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
	return cell;
}

#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSUInteger row = [indexPath row];
	
	if ([[self.options objectAtIndex:row] isEqualToString:@"Web Applications"]) {
		
		[self.navigationController.view addSubview:spinner.view];
		
		FetchJSONOperation *fetchJSON = [[FetchJSONOperation alloc] initJSONRequestForServer:server.hostname
																						  path:kWARPath
																						target:self
																				  targetMethod:@selector(processResponseFromServer:)];

		// add it to the queue
		[workQueue addOperation:fetchJSON];
		
		[fetchJSON release];		
		
	} else {
		UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle: @"Not implemented yet..."
							  message:nil
							  delegate:nil
							  cancelButtonTitle:@"Cancel"
							  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	
	[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -
#pragma mark JSON Response Callback
-(IBAction)processResponseFromServer:(id)response {
	[spinner.view removeFromSuperview];
	
	if ([response isKindOfClass:[NSDictionary class]])  {
		DeploymentsViewController *deploymentsController = [[DeploymentsViewController alloc] initWithStyle:UITableViewStylePlain];
		deploymentsController.title = @"War's";
		deploymentsController.response = response;
		
		[self.navigationController pushViewController:deploymentsController animated:YES];
		[deploymentsController release];

	} else {  // an error has occured, inform user
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!"
														message:response
													   delegate:nil 
											  cancelButtonTitle:@"Bummer"
											  otherButtonTitles:nil];
		
		[alert show];
		[alert release];
	}		
}

@end
